<template>
	<!-- 地图 -->
	<view>
		<!-- 导航 -->
		<u-navbar back-text="选择地址" :back-text-style="textStyle" back-icon-color="#000000" style="font-weight: bold;font-family: PingFang SC;"></u-navbar>
		<!-- <u-navbar back-icon-color="#000000" title="选择地址" title-color="#3F3F3F" :title-bold="true" :background="background"></u-navbar> -->
		<view>
			<view class="sreach" style="display: flex;flex-direction: row;align-items: center;position: relative;">
				<view style="border-right:2rpx solid #E4E4E4;" @click="jumpToSelectCity">
					<text class="ctiy">{{siteName}}</text>
					<u-icon name="arrow-down" color="#707070" size="28" style="margin-left: 14rpx;margin-right: 16rpx;"></u-icon>
				</view>
				<view style="margin-left: 24rpx;">
					<u-input  v-model="serachValue" type="text" :clearable="false" placeholder="搜索您所在的地址" confirm-type="搜索" @confirm="searchClick"/>
					<view
						@click="searchClick"
						style="top: 6rpx;right: 10rpx;color: white; position: absolute; border-radius: 32rpx; text-align: center;line-height: 58rpx; width:140rpx;height: 58rpx;background: linear-gradient(327deg, #F83011 0%, #FF9900 100%);font-size: 26rpx;"
					>
						搜索
					</view>
				</view>
			</view>

			<map id="maps" :latitude="latitude" :longitude="longitude" scale="16" @regionchange="regionchange" :markers='markers'></map>
			
			<scroll-view scroll-y="true" style="height: 785rpx;" >
				<view :key="count">
					<view class="tables" v-for="(item,index) in poisdatas" :key="index" @click="getAddress(item)">
						<view>
							<view class="typeface1" :style="index==0?'color:#FCA800;':'margin-left:33rpx;'">
								<image v-if="index==0" src="../../static/img/1-5.png" style="width: 21rpx;height: 30rpx;vertical-align: middle;margin-right: 12rpx;"></image>
								{{item.name}}
							</view>
							<view class="typeface2" style="margin-top: 6rpx;margin-left:33rpx;">{{item.cityname}}{{item.adname}}{{item.address}}</view>
						</view>
						<view>
							<view class="typeface3" style="margin-right:33rpx;">距离{{item.distance}}m</view>
						</view>
					</view>
				</view>
			</scroll-view>
			
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import amap from "../../components/map/amap-wx.js";
	export default {
		data() {
			return {
				background:{
					backgroundColor: '#fca800',
				},
				siteName:'',
				markers: [],
				latitude: null,
				longitude: null,
				serachValue:'',
				poisdatas:[],
				count:0,
				myAmapFun:null,
				type:1,//1：普通地址进入   2：非她莫属跳转进入
			}
		},
		onLoad(option) {
			var that = this;
			this.myAmapFun = new amap.AMapWX({
				key: that.http.mapVxKey
			});
			
			if(option.type){
				that.type=option.type;
			}
		},
		onShow() {
			var that = this;
			uni.getStorage({
				key:'siteName',
				success:(res)=>{
					that.siteName=res.data;
					that.serachValue=res.data;
					
					//获取地理位置
					uni.getLocation({
						type: 'wgs84',
						success(res) {
							// console.log("你当前经纬度是：");
							console.log(res);
							let latitude, longitude;
							latitude = res.latitude.toString();
							longitude = res.longitude.toString();
							var myAmapFun = new amap.AMapWX({
								key: that.http.mapVxKey
							});
							myAmapFun.getRegeo({
								location: '' + longitude + ',' + latitude + '', //location的格式为'经度,纬度'
								success: function(e) {
									let cityName = e[0].regeocodeData.addressComponent.city; //城市
									if(cityName == that.siteName){
										uni.getLocation({
											type: 'gcj02', //wgs84 地球坐标 (WGS84)  火星坐标 (GCJ-02)也叫国测局坐标系
											success: function(res) {
												that.latitude = res.latitude;
												that.longitude = res.longitude;
												console.log(res);
												that.getPOI({querykeywords:'',location:res.longitude+','+res.latitude});
											}
										});
									}else{
										that.searchClick();
									}
									
								},
								fail: function(info) {
									uni.getLocation({
										type: 'gcj02', //wgs84 地球坐标 (WGS84)  火星坐标 (GCJ-02)也叫国测局坐标系
										success: function(res) {
											that.latitude = res.latitude;
											that.longitude = res.longitude;
											console.log(res);
											that.getPOI({querykeywords:'',location:res.longitude+','+res.latitude});
										}
									});
								}
							});
						}
					});
					
				},
				fail() {
					uni.getLocation({
						type: 'gcj02', //wgs84 地球坐标 (WGS84)  火星坐标 (GCJ-02)也叫国测局坐标系
						success: function(res) {
							that.latitude = res.latitude;
							that.longitude = res.longitude;
							console.log(res);
							that.getPOI({querykeywords:'',location:res.longitude+','+res.latitude});
						}
					});
				}
			});
		},
		methods: {
			
			jumpToSelectCity:function(){
				uni.navigateTo({
					url:'../home/selectCity/selectCity?cityName='+this.siteName
				})
			},
			goBack: function() {
				uni.navigateBack();
			},
			regionchange(event) {

				if (event.type == 'end') {
					if(!this.serachValue){
						this.getCenterLanLat()
					}else{
						this.serachValue=null;
					}
				}
			},

			// 获取当前地图中心的经纬度
			getCenterLanLat() {
				var that=this;
				this.mapContext = uni.createMapContext("maps", this);
				this.mapContext.getCenterLocation({
					type: 'gcj02',
					success: (res) => {
						console.log(res.longitude)
						that.getPOI({querykeywords:'',location:res.longitude+','+res.latitude});

					},
					fail: (err) => {
						console.log(this.latitude)
						console.log('获取当前地图中心的经纬度2', err);
					}
				})
			},

			//根据地图坐标获取位置
			getPOI(params) {
				console.log(params)
				var that = this;
				this.myAmapFun.getPoiAround({
					querykeywords:params.querykeywords,
					location:params.location,
					success: function(data) {
						//成功回调
						that.poisdatas=data.poisData;
						// console.log(data)
						that.latitude= data.markers[0].latitude;
						that.longitude=data.markers[0].longitude;
						that.markers = [{
							id: 0,
							longitude: data.markers[0].longitude,
							latitude: data.markers[0].latitude,
							iconPath: '../../static/img/1-24.png',
							width: '26rpx',
							height: '52rpx',
							callout: {
								color: '#000',
								content: data.markers[0].name,
								fontSize: 12,
								borderRadius: 5,
								bgColor: '#ffffff',
								padding: 10,
								textAlign: 'center',
								display: "ALWAYS"
							}
						}]
						that.count+=1;
					},
					fail: function(info) {
						//失败回调
						console.log(info)
					}
				})
			},
			searchClick:function(){
				let that=this;
				if(typeof(that.siteName) !== "string"){
					that.siteName="郑州"
				}
					
				this.myAmapFun.getInputtips({
					keywords:that.serachValue,
					city:that.siteName == "" || !that.siteName?'郑州':that.siteName,
					success: function(data) {
						//成功回调
						/* console.log("搜索回调")
						console.log(data) */
						if(data.tips){
							for (let i = 0; i < data.tips.length; i++) {
								if(data.tips[i].location.length!=0){
									console.log(data.tips[i]);
									that.getPOI({querykeywords:'',location:data.tips[i].location});
									return true;
								}
							}
							
						}
					},
					fail: function(info) {
						//失败回调
						console.log("失败回调")
						console.log(info)
					},
					
				})
			},
			getAddress:function(item){
				console.log(item)
				let opts = {
					way: true, //true JSON请求；false 表单请求
					method: 'post',
					url: 'user/judgeOpen'
				};
				let data = {
					cityName: item.cityname
				};
				this.http.httpTokenRequest(opts, data).then(res => {
					if(res.code=="200"){
						if(res.data.isOpen==1){
							let pages = getCurrentPages();  //获取所有页面栈实例列表
							let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
							prevPage.$vm.province = item.pname;
							prevPage.$vm.city = item.cityname;
							prevPage.$vm.region = item.adname;
							prevPage.$vm.remnant =item.address.length+item.name.length ;
							
							prevPage.$vm.address = item.address+item.name;
							
							if(this.type==1){
								prevPage.$vm.label =item.pname+item.cityname+item.adname;
							}
							prevPage.$vm.mapAddress=item.address;
							uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
								delta: 1
							});
						}else{
							this.$refs.uToast.show({
								title:"该城市暂未开通",
								type: 'info'
							})
						}
					}else{
						this.$refs.uToast.show({
							title: res.message,
							type: 'error'
						})
					}
				});
			}

		}
	}
</script>

<style>
	.sreach{
		margin: 42rpx;
		width: 666rpx;
		height: 76rpx;
		background: #FFFFFF;
		border: 2rpx solid #E4E4E4;
		box-shadow: 0rpx 6rpx 12rpx rgba(0, 0, 0, 0.16);
		opacity: 1;
		border-radius: 38rpx;
		padding: 18rpx 28rpx;
		
	}
	#maps {
		margin: 40rpx;
		width: 668rpx;
		height: 536rpx;
		background: rgba(0, 0, 0, 0);
		box-shadow: 0rpx 6rpx 12rpx rgba(0, 0, 0, 0.16);
		opacity: 1;
		border-radius: 10rpx;
	}
	.ctiy{
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #707070;
		opacity: 1;
	}
	.typeface1{
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #707070;
		opacity: 1;
	}
	.typeface2{
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #707070;
		opacity: 1;
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		width: 520rpx;
	}
	.typeface3{
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #707070;
		opacity: 1;
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		width: 520rpx;
	}
	.tables{
		margin: 19rpx 40rpx;
		display: flex;
		justify-content:space-between;
		padding: 10rpx 0rpx;
		border-bottom: 2rpx solid #E4E4E4;
	}
</style>
